import { gameConfigHandle } from "core/config/gameConfig"
import gameUtils from "core/utils/gameUtils"
import { GameRouteData, GameTime, localApi } from "core/utils/localApi"
import utils from "core/utils/utils"
import { BattleResult } from "core/typeDefine/typeDefine"
import equipmentHandle from "core/handle/equipmentHandel"
import bagHandle from "core/handle/bagHandle"
import { DiaryModel } from "core/model/diaryModel"
import playerHandle from "core/handle/playerHandle"
import adventureHandle from "core/handle/adventureHandle"
import marketHandle from "core/handle/marketHandle"
import { MapExploreDataModel } from "core/model/adventureModel"
import { base64 } from "core/utils/enco"
import luckHandle from "core/handle/luckHandle"
import Random from "core/utils/random"


class AdventureRoute {
    @localApi("adventureRoute")
    auto(gameRouteData?: GameRouteData) {
        let { player, time, random, version, vipLvl, isMonthVip,isYearVip, dayTime } = gameRouteData

        if (player.baseInfo.dayTime == 0) {
            player.baseInfo.dayTime = dayTime
        }
        if (player.baseInfo.dayTime < dayTime) {
            playerHandle.resetDay(player, time, dayTime)
            player.baseInfo.dayTime = dayTime
        }
        if (player.adventure.getResourceTime == 0) {
            player.adventure.getResourceTime = time
            return { err: 0 }
        }
        player.adventure.offlineTime = time
        if (player.adventure.autoTime == 0) {
            player.adventure.autoTime = time
            return { err: 0 }
        }

        let getTimes = Math.floor((time - player.adventure.autoTime) / 60000)
        if (getTimes > 0) {
            player.adventure.autoTime += getTimes * 60000
        }

        let adventureConfig = gameConfigHandle.gameConfig.adventure
        player.adventure.mapExploreData.forEach(mapExploreDataItem => {
            if (mapExploreDataItem) {
                let mapItemConfig = gameConfigHandle.configByID[mapExploreDataItem.mapID]
                let getTimes = Math.floor((time - mapExploreDataItem.exploreTime) / (adventureConfig.exploreInterval * mapItemConfig.exploreDiffculty))
                if (getTimes > 0) {
                    mapExploreDataItem.exploreTime += getTimes * adventureConfig.exploreInterval * mapItemConfig.exploreDiffculty
                    if (!player.adventure.mapExploreProgress[mapExploreDataItem.mapID]) {
                        player.adventure.mapExploreProgress[mapExploreDataItem.mapID] = 0
                    }

                    player.adventure.mapExploreProgress[mapExploreDataItem.mapID] += getTimes

                    mapItemConfig.getItems.forEach(item => {
                        if (random.intn(100) <= item.rate) {
                            if (item.itemID) {
                                let values = item.incValues
                                let value = utils.getIncValues(values, 1)
                                value = Math.floor(value * getTimes)
                                bagHandle.addItem(player.bag, item.itemID, value)
                                if (!mapExploreDataItem.getData[item.itemID]) {
                                    mapExploreDataItem.getData[item.itemID] = 0
                                }
                                mapExploreDataItem.getData[item.itemID] += value
                            }
                            if (item.itemIDs) {
                                let itemID: number = utils.getOneFromArray(item.itemIDs, random)
                                let values = item.incValues
                                let value = utils.getIncValues(values, 1)
                                value = Math.floor(value * getTimes)
                                bagHandle.addItem(player.bag, itemID, value)
                                if (!mapExploreDataItem.getData[item.itemID]) {
                                    mapExploreDataItem.getData[item.itemID] = 0
                                }
                                mapExploreDataItem.getData[itemID] += value
                            }
                        }
                    })

                    if (player.adventure.mapExploreProgress[mapExploreDataItem.mapID] >= mapItemConfig.maxExplore) {
                        player.adventure.mapExploreProgress[mapExploreDataItem.mapID] = mapItemConfig.maxExplore
                        mapItemConfig.exploreMaxGetItems && mapItemConfig.exploreMaxGetItems.forEach(item => {
                            for (let i = 0; i < getTimes; i++) {
                                if (random.intn(100) <= item.rate) {
                                    if (item.itemID) {
                                        let values = item.incValues
                                        let value = utils.getIncValues(values, 1)
                                        value = Math.floor(value)
                                        bagHandle.addItem(player.bag, item.itemID, value)
                                        if (!mapExploreDataItem.getData[item.itemID]) {
                                            mapExploreDataItem.getData[item.itemID] = 0
                                        }
                                        mapExploreDataItem.getData[item.itemID] += value
                                    }
                                    if (item.itemIDs) {
                                        let itemID: number = utils.getOneFromArray(item.itemIDs, random)
                                        let values = item.incValues
                                        let value = utils.getIncValues(values, 1)
                                        value = Math.floor(value)
                                        bagHandle.addItem(player.bag, itemID, value)
                                        if (!mapExploreDataItem.getData[item.itemID]) {
                                            mapExploreDataItem.getData[item.itemID] = 0
                                        }
                                        mapExploreDataItem.getData[itemID] += value
                                    }
                                }
                            }
                        })
                    }
                }
            }
        })
        playerHandle.initTeamFighterAttri(player, isMonthVip,isYearVip)
        // player.adventure.autoTime = time
        return { err: 0 }
        // return { err: 0, data: { getTimes: getTimes, offlineDiary: offlineDiary, offlineTime } }
    }


    @localApi("adventureRoute")
    adventureBattle(gameRouteData?: GameRouteData) {
        let { player, time, random, version, vipLvl, isMonthVip, dayTime } = gameRouteData
        let floor = player.adventure.floor || 1
        let mapID = player.adventure.mapID
        let { monsterLvl } = adventureHandle.getMapMonsterLvl(mapID, floor)
        let seed = random.getInitSeed()
        let speed = 1
        let altarDamageCoe = 1
        if (player.adventure.speedUpTime > 0) {
            let speedItemConfig = gameConfigHandle.gameConfig.adventure.speedUpItemList[player.adventure.speedUpIndex]
            speed = speedItemConfig.speed
            altarDamageCoe = speedItemConfig.altarDamage
        }
        return {
            err: 0,
            data: {
                seed, mapID: mapID, floor: floor, monsterLvl, speed, altarDamageCoe
            }
        }
    }

    @localApi("adventureRoute")
    adventureSettle(result: BattleResult, floor: number, totalRunTime, speedRunTime, routeDataStr, gameRouteData?: GameRouteData) {
        let { player, random, time } = gameRouteData
        let routeData = JSON.parse(base64.decode(routeDataStr))
        let code = 0
        let getData = { equipmentAmount: 0, mapGet: {} }
        if (routeData.floor != player.adventure.floor) {
            return { err: 100 }
        }
        if (player.adventure.speedUpTime > 0 && speedRunTime > 0) {
            player.adventure.speedUpTime -= Math.floor(speedRunTime / 1000 + 4)

            if (player.adventure.speedUpTime < 0) {
                player.adventure.speedUpTime = 0
            }
        }
        player.adventure.offlineTime += Math.floor(totalRunTime / 1000 + 3)
        if (player.adventure.offlineTime > time) {
            player.adventure.offlineTime = time
        }

        if (routeData.result == BattleResult.Win) {
            let { monsterLvl, monsterMaxLvl } = adventureHandle.getMapMonsterLvl(player.adventure.mapID, player.adventure.floor)
            adventureHandle.adventureSettle(player, monsterMaxLvl, getData, random)
            player.adventure.floor += 1
            let mapID = player.adventure.mapID
            let mapItemConfig = gameConfigHandle.configByID[mapID]
            if (!player.adventure.mapProgress[mapID]) {
                player.adventure.mapProgress[mapID] = 0
            }
            if (player.adventure.floor > mapItemConfig.maxFloor) {
                if (mapItemConfig.nextMapID) {
                    player.adventure.mapID = mapItemConfig.nextMapID
                    player.adventure.floor = 1
                    if (!player.adventure.mapProgress[mapItemConfig.nextMapID]) {
                        player.adventure.mapProgress[mapItemConfig.nextMapID] = 1
                    }
                    code = 1
                } else {
                    if (mapItemConfig.endless) {
                        player.adventure.mapProgress[mapID] = player.adventure.floor
                    } else {
                        player.adventure.floor = mapItemConfig.maxFloor
                    }
                }
            } else if (player.adventure.mapProgress[mapID] < player.adventure.floor) {
                player.adventure.mapProgress[mapID] = player.adventure.floor
            }
        }

        return { err: 0, data: getData, code }
    }

    @localApi("adventureRoute")
    adventurOfflineSettle(initFloor: number, endFloor: number, gameRouteData?: GameRouteData) {
        let { player, random } = gameRouteData

        let getData = { equipmentAmount: 0, skillBookAmount: 0, mapGet: {} }
        if (initFloor != player.adventure.floor) {
            return { err: 100 }
        }
        for (let floor = initFloor; floor < endFloor; floor++) {
            adventureHandle.adventureSettle(player, floor, getData, random)
            // if ((floor - 1) == gameConfigHandle.gameConfig.power.market.unlock) {
            //     marketHandle.create(player, random)
            // }
        }
        player.adventure.floor = endFloor
        return { err: 0, data: getData }
    }

    @localApi("adventureRoute")
    instanceBattle(mapID: number, gameRouteData?: GameRouteData) {
        let { player, time, random, version, vipLvl, isMonthVip, dayTime } = gameRouteData
        let floor = player.adventure.mapProgress[mapID] || 1
        let mapConfig = gameConfigHandle.gameConfig.map
        let seed = random.getInitSeed()
        let mapItemConfig = gameConfigHandle.configByID[mapID]

        let maxTimes = adventureHandle.getInstanceAmount(player, isMonthVip)

        if (player.adventure.instanceAmount >= maxTimes) {
            return { err: 1 }
        }
        if ((player.adventure.mapDayChallenge[mapID] || 0) >= mapConfig.singleInstanceDayAmount) {
            return { err: 4 }
        }

        if (mapItemConfig.getEquipments) {
            if (!bagHandle.isHasEmpty(player.equipmentBag, mapItemConfig.getEquipments)) {
                return { err: 2 }
            }
        }

        if (mapItemConfig.getSkillBook) {
            if (!bagHandle.isHasEmpty(player.skillBag, mapItemConfig.getSkillBook)) {
                return { err: 3 }
            }
        }
        if (mapItemConfig.getPet) {
            if (!bagHandle.isHasEmpty(player.petBag, 1)) {
                return { err: 5 }
            }
        }
        let monsterLvl = utils.getIncValues(mapItemConfig.monsterLvl, floor)

        let { mapGet, mapGetPet } = adventureHandle.instanceSettleItem(player, mapID, floor, random)
        player.adventure.instanceAmount += 1
        player.adventure.mapDayChallenge[mapID] = (player.adventure.mapDayChallenge[mapID] || 0) + 1
        return {
            err: 0,
            data: {
                seed, mapID: mapID, floor: floor, monsterLvl, mapGet, mapGetPet, equipmentAmount: mapItemConfig.getEquipments || 0, skillBookAmount: mapItemConfig.getSkillBook || 0,
            }
        }
    }

    @localApi("adventureRoute")
    instanceComplete(mapID: number, gameRouteData?: GameRouteData) {
        let { player, time, random, version, vipLvl, isMonthVip, dayTime } = gameRouteData
        let floor = player.adventure.mapProgress[mapID] || 1

        let seed = random.getInitSeed()
        let mapItemConfig = gameConfigHandle.configByID[mapID]

        let mapConfig = gameConfigHandle.gameConfig.map
        let maxTimes = adventureHandle.getInstanceAmount(player, isMonthVip)
        if (player.adventure.instanceAmount >= maxTimes) {
            return { err: 1 }
        }

        let dayTimes = player.adventure.mapDayChallenge[mapID] || 0
        if (dayTimes >= mapConfig.singleInstanceDayAmount) {
            return { err: 4 }
        }

        if (mapItemConfig.getEquipments) {
            if (!bagHandle.isHasEmpty(player.equipmentBag, mapItemConfig.getEquipments)) {
                return { err: 2 }
            }
        }

        if (mapItemConfig.getSkillBook) {
            if (!bagHandle.isHasEmpty(player.skillBag, mapItemConfig.getSkillBook)) {
                return { err: 3 }
            }
        }

        if (mapItemConfig.getPet) {
            if (!bagHandle.isHasEmpty(player.petBag, 1)) {
                return { err: 5 }
            }
        }

        let { mapGet, mapGetPet } = adventureHandle.instanceSettleItem(player, mapID, floor, random)
        player.adventure.instanceAmount += 1
        player.adventure.mapDayChallenge[mapID] = (player.adventure.mapDayChallenge[mapID] || 0) + 1
        return {
            err: 0,
            data: {
                seed, mapID: mapID, floor: floor, mapGet, mapGetPet, equipmentAmount: mapItemConfig.getEquipments || 0, skillBookAmount: mapItemConfig.getSkillBook || 0,
            }
        }
    }

    @localApi("adventureRoute")
    instanceSettle(mapID: number, floor: number, result: BattleResult, routeDataStr: any, gameRouteData?: GameRouteData) {
        let { player, time, random, version, vipLvl, isMonthVip, dayTime } = gameRouteData
        let _floor = player.adventure.mapProgress[mapID] || 1
        if (floor !== _floor) {
            return { err: 101 }
        }
        let routeData = JSON.parse(base64.decode(routeDataStr))
        if (routeData.result == BattleResult.Win) {
            let mapItemConfig = gameConfigHandle.configByID[mapID]
            if (floor >= mapItemConfig.maxFloor) {
                return { err: 1 }
            }
            if (!player.adventure.mapProgress[mapID]) {
                player.adventure.mapProgress[mapID] = 1
            }
            player.adventure.mapProgress[mapID] += 1
        }
        return {
            err: 0
        }
    }

    @localApi("adventureRoute")
    bossBattle(gameRouteData?: GameRouteData) {
        let { player, time, random, version, vipLvl, isMonthVip, dayTime } = gameRouteData
        let mapConfig = gameConfigHandle.gameConfig.map
        let mapID = mapConfig.bossMapID
        let mapItemConfig = gameConfigHandle.configByID[mapID]
        let floor = player.adventure.mapProgress[mapID] || 1

        let seed = random.getInitSeed()
        let maxTimes = adventureHandle.getBossAmount(player, isMonthVip)
        if (player.adventure.bossMapData.bossTimes >= maxTimes) {
            return { err: 1 }
        }
        if (mapItemConfig.getEquipments) {
            if (!bagHandle.isHasEmpty(player.equipmentBag, mapItemConfig.getEquipments)) {
                return { err: 2 }
            }
        }

        if (mapItemConfig.getSkillBook) {
            if (!bagHandle.isHasEmpty(player.skillBag, mapItemConfig.getSkillBook)) {
                return { err: 3 }
            }
        }
        if (mapItemConfig.getPet) {
            if (!bagHandle.isHasEmpty(player.petBag, 1)) {
                return { err: 5 }
            }
        }
        let monsterLvl = utils.getIncValues(mapItemConfig.monsterLvl, floor)

        let { mapGet, mapGetPet } = adventureHandle.instanceSettleItem(player, mapID, floor, random)
        player.adventure.bossMapData.bossTimes += 1
        player.adventure.mapDayChallenge[mapID] = (player.adventure.mapDayChallenge[mapID] || 0) + 1
        return {
            err: 0,
            data: {
                seed, mapID: mapID, floor: floor, monsterLvl, dayTime,
                mapGet, mapGetPet, equipmentAmount: mapItemConfig.getEquipments || 0, skillBookAmount: mapItemConfig.getSkillBook || 0,
            }
        }
    }
    @localApi("adventureRoute")
    bossSettle(floor: number, result: BattleResult, reduceHpOfBoss: number, routeDataStr, gameRouteData?: GameRouteData) {
        let { player, time, random, version, vipLvl, isMonthVip, dayTime } = gameRouteData
        let routeData = JSON.parse(base64.decode(routeDataStr))
        let mapConfig = gameConfigHandle.gameConfig.map
        let bossMapID = mapConfig.bossMapID
        let maxHurt = player.adventure.bossMapData.reduceBossHpMax
        if (routeData.reduceHpOfBoss > maxHurt) {
            player.adventure.bossMapData.reduceBossHpMax = routeData.reduceHpOfBoss
        }
        return {
            err: 0
        }
    }

    @localApi("adventureRoute")
    abyssBattle(gameRouteData?: GameRouteData) {
        let { player, time, random, version, vipLvl, isMonthVip, dayTime } = gameRouteData
        let mapConfig = gameConfigHandle.gameConfig.map
        let abyssMapID = mapConfig.abyssMapID
        let floor = player.adventure.mapProgress[abyssMapID] || 1

        let seed = random.getInitSeed()
        // let maxTimes = adventureHandle.getAbyssAmount(player, isMonthVip)

        // if (player.adventure.abyssAmount >= maxTimes) {
        //     return { err: 1 }
        // }
        let needConfig = gameConfigHandle.gameConfig.need
        let res = gameUtils.useNeedResource(player, needConfig.challengeTower, 1)
        if (res !== 0) {
            return { err: 1 }
        }
        let mapItemConfig = gameConfigHandle.configByID[abyssMapID]
        let monsterLvl = utils.getIncValues(mapItemConfig.monsterLvl, floor)
        player.adventure.abyssAmount += 1
        if (!player.adventure.mapDayChallenge[abyssMapID]) {
            player.adventure.mapDayChallenge[abyssMapID] = 1
        }
        player.adventure.mapDayChallenge[abyssMapID] += 1
        return {
            err: 0,
            data: {
                seed, mapID: abyssMapID, floor: floor, monsterLvl
            }
        }
    }

    @localApi("adventureRoute")
    abyssSettle(floor: number, result: BattleResult, gameRouteData?: GameRouteData) {
        let { player, time, random, version, vipLvl, isMonthVip, dayTime } = gameRouteData
        let mapConfig = gameConfigHandle.gameConfig.map
        let abyssMapID = mapConfig.abyssMapID
        let _floor = player.adventure.mapProgress[abyssMapID] || 1
        let mapItemConfig = gameConfigHandle.configByID[abyssMapID]
        if (floor !== _floor) {
            return { err: 101 }
        }
        let mapGet = {}
        if (result == BattleResult.Win) {
            if (!player.adventure.mapProgress[abyssMapID]) {
                player.adventure.mapProgress[abyssMapID] = 1
            }
            player.adventure.mapProgress[abyssMapID] += 1

            if (mapItemConfig.abyssDropItems) {
                mapItemConfig.abyssDropItems.forEach(item => {
                    if (item.itemIDs && floor >= item.limitFloor) {
                        if (item.interval && (floor - item.limitFloor) % 3 !== (item.interval - 1)) {
                            return
                        }
                        let itemIDs: number[] = utils.getRandomArrayNoRepeat(item.itemIDs, item.amount, random)
                        itemIDs.forEach(itemID => {
                            let values = item.itemAmount
                            let value = utils.getIncValues(values, floor)
                            if (value > item.maxAmount) {
                                value = item.maxAmount
                            }

                            if (!mapGet[itemID]) {
                                mapGet[itemID] = 0
                            }
                            mapGet[itemID] += value
                            bagHandle.addItem(player.bag, itemID, value)
                        })
                    }
                })
            }
            if (mapItemConfig.getResource) {
                mapItemConfig.getResource.forEach(item => {
                    let itemID = item.itemID
                    let values = item.incValues
                    let value = adventureHandle.getGoldResoureOriginAdd(player) * utils.getIncValues(values, floor)
                    value = Math.floor(value)
                    if (!mapGet[itemID]) {
                        mapGet[itemID] = 0
                    }
                    mapGet[itemID] += value
                    if (typeof itemID == 'number') {
                        bagHandle.addItem(player.bag, itemID, value)
                    } else {
                        player.itemInfo[itemID] += value
                    }
                })
            }
        }

        return {
            err: 0,
            data: {
                mapGet
            }
        }
    }

    @localApi("adventureRoute")
    exploreMap(mapID: number, gameRouteData?: GameRouteData) {
        let { player, time } = gameRouteData
        let mapExploreData = new MapExploreDataModel()
        let index = player.adventure.mapExploreData.findIndex(item => item.mapID == mapID)
        if (index !== -1) {
            player.adventure.mapExploreData.splice(index, 1)
            return {
                err: 0
            }
        }
        let mapExploreMaxAmount = adventureHandle.getAdventureExploreIngMaxAmount(player)
        if (player.adventure.mapExploreData.length >= mapExploreMaxAmount) {
            return {
                err: 1
            }
        }
        mapExploreData.mapID = mapID
        mapExploreData.startTime = time
        mapExploreData.exploreTime = time
        player.adventure.mapExploreData.push(mapExploreData)
        return {
            err: 0
        }
    }

    @localApi("adventureRoute")
    getOnlineData(gameRouteData?: GameRouteData) {
        let { player, time, isMonthVip, isYearVip, random } = gameRouteData
        let maxHour = adventureHandle.getOnlineMaxTime(player, isMonthVip)
        let seconds = Math.floor((time - player.adventure.getResourceTime) / 1000)
        let getTimes = Math.floor(seconds / 60)
        if (getTimes < 1) {
            return { err: 1 }
        }
        if (getTimes >= maxHour * 60) {
            getTimes = maxHour * 60
        }

        let offlineDiary = new DiaryModel()
        let offlineTime = time - player.adventure.getResourceTime

        let { total, maxTotal } = adventureHandle.getAdventureMapProgress(player)
        let adventureConfig = gameConfigHandle.gameConfig.adventure
        if (getTimes > 0) {
            let gold = adventureHandle.getGoldResoureAdd(player, isMonthVip, isYearVip) * getTimes
            player.itemInfo['gold'] += gold
            offlineDiary['gold'] += gold
            adventureConfig.autoDropItems.forEach(item => {
                if (item.limitFloor) {
                    if (maxTotal < item.limitFloor) {
                        return
                    }
                }
                if (item.limitMapID) {
                    if (gameUtils.isMapLimit(player, item.limitMapID)) {
                        return
                    }
                }

                let amount = 0
                for (let i = 0; i < getTimes; i++) {
                    if (random.intn(100) <= item.rate && (player.adventure.resourceTimes + i) % item.dropInterval == 0) {
                        let _amount = utils.getIncValues(item.amount, maxTotal)
                        if (_amount > item.maxAmount) {
                            _amount = item.maxAmount
                        }
                        amount += _amount
                    }
                }
                if (amount == 0) {
                    return
                }
                let itemIDsGet = {}
                for (let i = 0; i < amount; i++) {
                    let itemID
                    // if (item.itemIDs) {
                    //     itemID = utils.getOneFromArray(item.itemIDs, random)
                    // } else 
                    if (item.itemID) {
                        itemID = item.itemID
                    }
                    if (!itemIDsGet[itemID]) {
                        itemIDsGet[itemID] = 0
                    }
                    itemIDsGet[itemID] += 1

                }
                utils.entries(itemIDsGet).forEach(([itemIDStr, amount]) => {
                    if (!isNaN(Number(itemIDStr))) {
                        let itemID = parseInt(itemIDStr)
                        bagHandle.addItem(player.bag, itemID, amount as number)
                        if (!offlineDiary.items[itemID]) {
                            offlineDiary.items[itemID] = 0
                        }
                        offlineDiary.items[itemID] += amount
                    } else {
                        let itemID = itemIDStr
                        player.itemInfo[itemID] += amount
                        if (!offlineDiary.items[itemID]) {
                            offlineDiary.items[itemID] = 0
                        }
                        offlineDiary.items[itemID] += amount
                    }
                })
            })
            let equipmentdropAmount = Math.floor((player.adventure.resourceTimes + getTimes) / adventureConfig.equipmentdropInterval) - Math.floor(player.adventure.resourceTimes / adventureConfig.equipmentdropInterval)
            if (equipmentdropAmount > 0) {
                //套装增加装备掉落
                equipmentdropAmount = Math.floor(equipmentdropAmount * (1 + player.adventure.addEquipmentDropPer / 100))
                for (let i = 0; i < equipmentdropAmount; i++) {
                    let equipmentLvl: number = equipmentHandle.getEquipmentLvl(maxTotal)
                    let equipmentItem = equipmentHandle.create(player, null, random, { equipmentLvl })
                    let destroy = false
                    if (!offlineDiary.equipments[equipmentItem.equipment.quality]) {
                        offlineDiary.equipments[equipmentItem.equipment.quality] = 0
                    }
                    offlineDiary.equipments[equipmentItem.equipment.quality] += 1
                    if (player.adventure.equipmentAutoDestroy[equipmentItem.equipment.quality]) {
                        destroy = true
                    } else {
                        let res = bagHandle.addItemComponent(player.equipmentBag, equipmentItem)
                        if (!res) {
                            destroy = true
                        }
                    }
                    if (destroy) {
                        if (offlineDiary) {
                            if (!offlineDiary.equipmentsDestroy[equipmentItem.equipment.quality]) {
                                offlineDiary.equipmentsDestroy[equipmentItem.equipment.quality] = 0
                            }
                            offlineDiary.equipmentsDestroy[equipmentItem.equipment.quality] += 1
                        }
                        equipmentHandle.destroy(player, equipmentItem)
                    }

                }
            }


            player.adventure.getResourceTime += getTimes * adventureConfig.getResourceInterval
            player.adventure.resourceTimes += getTimes
            if (getTimes >= maxHour * 60) {
                player.adventure.getResourceTime = time
            }
            player.baseInfo.getOnlineDataTimes += getTimes
        }



        return { err: 0, data: { getTimes: getTimes, offlineDiary: offlineDiary, offlineTime } }
    }

    @localApi("adventureRoute")
    clickGetGold(gameRouteData?: GameRouteData) {
        let { player, time, isMonthVip,isYearVip, random } = gameRouteData
        let adventureConfig = gameConfigHandle.gameConfig.adventure
        if (player.baseInfo.clickGetGoldTimes >= adventureConfig.clickGetGoldFreeTimes) {
            let clickGetGoldMaxTimes = adventureHandle.getClickGetGoldMaxTimes(player, isMonthVip)
            if (player.baseInfo.clickGetGoldTimes >= clickGetGoldMaxTimes + adventureConfig.clickGetGoldFreeTimes) {
                return { err: 50 }
            } else {
                let needConfig = gameConfigHandle.gameConfig.need
                let res = gameUtils.useNeedResource(player, needConfig.clickGetGoldTimes, 1)
                if (res !== 0) {
                    return { err: 1 }
                }
            }
        }
        player.baseInfo.clickGetGoldTimes += 1
        let gold = adventureHandle.getGoldResoureAdd(player, isMonthVip,isYearVip) * (adventureConfig.clickGetGold + player.adventure.clickGold)
        player.itemInfo.gold += gold
        return { err: 0, data: gold }
    }

    @localApi("adventureRoute")
    useSpeedUp(index: number, gameRouteData?: GameRouteData) {
        let { player, time, isMonthVip, random } = gameRouteData
        let adventureConfig = gameConfigHandle.gameConfig.adventure
        let speedUpItemConfig = adventureConfig.speedUpItemList[index]
        if (!speedUpItemConfig) {
            return { err: 50 }
        }
        let itemID = speedUpItemConfig.itemID
        if (bagHandle.getTotalNumByItemId(player.bag, itemID) < 1) {
            return { err: 2 }
        }
        bagHandle.removeItemByItemId(player.bag, itemID, 1)

        if (player.adventure.speedUpIndex !== index) {
            player.adventure.speedUpIndex = index
            player.adventure.speedUpTime = speedUpItemConfig.duration * 60
        } else {
            player.adventure.speedUpIndex = index
            player.adventure.speedUpTime += speedUpItemConfig.duration * 60
        }
        return { err: 0, data: speedUpItemConfig.speed }
    }

    @localApi("adventureRoute")
    clearOfflineTime(gameRouteData?: GameRouteData) {
        let { player, time, random, version, vipLvl, isMonthVip, dayTime } = gameRouteData
        player.adventure.offlineTime = time
        return {
            err: 0
        }
    }

    @localApi("adventureRoute")
    swithOfflineSpeedUp(gameRouteData?: GameRouteData) {
        let { player, time, random, version, vipLvl, isMonthVip, dayTime } = gameRouteData
        player.adventure.offlineSpeedUp = !player.adventure.offlineSpeedUp
        return {
            err: 0
        }
    }

    @localApi("adventureRoute")
    frontBattle(index: number, gameRouteData?: GameRouteData) {
        let { player, time, random, version, vipLvl, isMonthVip, dayTime } = gameRouteData
        let map = player.luck.front.items[index]
        let abyssMapID = gameConfigHandle.gameConfig.luck.frontMaps[map]
        let frontMonsterLvlUp = gameConfigHandle.gameConfig.luck.frontMonsterLvlUp
        let floor = (player.luck.front.lvl + frontMonsterLvlUp[0]) * frontMonsterLvlUp[1] || frontMonsterLvlUp[0] * frontMonsterLvlUp[1]
        let seed = random.getInitSeed()
        let remain = luckHandle.getExplorePower(player, vipLvl, isMonthVip).remain
        if (remain < 1) {
            return { err: 1 }
        }
        player.luck.front.times++
        let mapItemConfig = gameConfigHandle.configByID[abyssMapID]
        let monsterLvl = utils.getIncValues(mapItemConfig.monsterLvl, floor)
        return {
            err: 0,
            data: {
                seed, mapID: abyssMapID, floor: floor, monsterLvl
            }
        }
    }

    @localApi("adventureRoute")
    frontSettle(index: number, floor: number, result: BattleResult, deadHeros: number[], gameRouteData?: GameRouteData) {
        let { player, time, random, version, vipLvl, isMonthVip, dayTime } = gameRouteData
        let front = player.luck.front
        let map = front.items[index]
        let abyssMapID = gameConfigHandle.gameConfig.luck.frontMaps[map]
        let frontMonsterLvlUp = gameConfigHandle.gameConfig.luck.frontMonsterLvlUp
        let _floor = (front.lvl + frontMonsterLvlUp[0]) * frontMonsterLvlUp[1] || frontMonsterLvlUp[0] * frontMonsterLvlUp[1]
        let mapItemConfig = gameConfigHandle.configByID[abyssMapID]
        if (floor !== _floor) {
            return { err: 101 }
        }
        let mapGet = {}
        let isComplete = false
        if (result == BattleResult.Win) {
            player.itemInfo.frontScore += 2
            mapGet['frontScore'] = 2
            front.items[index] = 5
            if (luckHandle.isFrontExploreComplete(player)) {
                isComplete = true
                luckHandle.resetFrontItems(player, random)
                let lvlMax = gameConfigHandle.gameConfig.luck.frontLvlMax
                if (front.lvl < lvlMax) {
                    front.lvl++
                }
                if (front.lvlMax < front.lvl) {
                    front.lvlMax = front.lvl
                }
            }
        } else {
            player.itemInfo.frontScore += 1
            mapGet['frontScore'] = 1
        }
        deadHeros.forEach(id => {
            if (!front.deadHeros.includes(id)) {
                front.deadHeros.push(id)
            }
        })
        return {
            err: 0,
            data: {
                mapGet, isComplete
            }
        }
    }
}

let adventureRoute: AdventureRoute = new AdventureRoute()
export default adventureRoute